//
//  BaseModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
import MBProgressHUD
enum SecurityState: String {
    case unboundStates = "未绑定"
    case unUsed = "未使用"
    case unVerified = "未验证"
}

typealias success = (_ data: [String : Any]?) ->()
typealias fail = (_ request: BaseRequest) ->()
class BaseViewModel: NSObject {
    typealias Response = (_ resState: ResponseState,_ isSuccess: Bool,_ msg: String?)->()
    var response: Response?
    var successBlock:success?
    var failBlock:fail?
    var successBlockDic : NSMutableDictionary = [:]
    var failBlockDic : NSMutableDictionary = [:]
    weak var controller:BaseViewController?
    
    lazy var cellfactory:TableCellFactory = {
        let cell = TableCellFactory()
        return cell
    }()
    
    override init() {
        super.init()
    }
    
    
    /// 初始化传入视图控制器
    ///
    /// - Parameter controller: controller
    init(controller:BaseViewController) {
        super.init()
        self.controller = controller
    }
    
    /// 网络请求
    ///
    /// - Parameters:
    ///   - success: 回调
    ///   - fail: 错误回调
    func request(request:BaseRequest, success:@escaping success, fail:@escaping fail) {
        objc_sync_enter(self)
        successBlockDic.setValue(success, forKey: request.path())
        failBlockDic.setValue(fail, forKey: request.path())
        self.successBlock = success
        self.failBlock = fail
        request.loadData()
        objc_sync_exit(self)
    }
}
